﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddSubCommitteeMember : System.Web.UI.Page
    {
        MemberAL memberAL = new MemberAL();
        public Guid SelectedMember
        {
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }

        public Guid SelectedSubCommittee
        {
            get
            {
                if (Session["SubCommitteeId"] == null)
                {
                    return memberAL.GetAllSubCommittees()[0].SubCommitteeId;
                }
                else
                {
                    return new Guid(Session["SubCommitteeId"].ToString());
                }
            }
            set
            {
                Session["SubCommitteeId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<SubCommitteeDTO> subCommitteeDTO = memberAL.GetAllSubCommittees();
            List<MemberDTO> memberDTO = memberAL.GetAllMembers();
            ddlSubCommMembrCommId.DataSource = subCommitteeDTO;
            ddlSubCommMembrCommId.DataTextField = "SubCommitteeName";
            ddlSubCommMembrCommId.DataValueField = "SubCommitteeId";
            ddlSubCommMembrCommId.SelectedValue = SelectedSubCommittee.ToString();
            ddlSubCommMembrCommId.DataBind();


            ddlSubCommMembrMembrId.DataSource = memberDTO;
            ddlSubCommMembrMembrId.DataTextField = "FullName";
            ddlSubCommMembrMembrId.DataValueField = "MemberId";
            ddlSubCommMembrMembrId.SelectedValue = SelectedMember.ToString();
            ddlSubCommMembrMembrId.DataBind();
           
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubCommitteeMemberSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SubCommitteeMemberDTO subcommitteeMemberDTO = new SubCommitteeMemberDTO
                {
                    SubCommitteId = new Guid(ddlSubCommMembrCommId.SelectedValue),
                    MemberId = new Guid(ddlSubCommMembrMembrId.SelectedValue),
                    Role = ddlSubCommitteeMemberMemType.SelectedValue,
                    //ModifiedBy = "Tharsan",
                    ModifiedTime = DateTime.Now
                };
                MemberAL memberAL = new MemberAL();
                string result = memberAL.saveSubCommitteeMember(subcommitteeMemberDTO); if (result == "Saved")
                {
                    lblAddSubCommMembrMsg.Text = "The Sub Committee Member Details added Successfully";
                    

                }
                else if (result == "Updated")
                {
                    lblAddSubCommMembrMsg.Text = "The Sub Committee Member Details Updated Successfully";
                   
                }
              
                else
                {
                    lblAddSubCommMembrMsg.Text = "Your data cannot be added";
                }
            }
            catch (Exception ee)
            {
                lblAddSubCommMembrMsg.Text = "There is an exception occured while inserting the Sub Committee Member details " + ee.ToString();
            }

        }

        protected void ddlCommMembrCommId_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedSubCommittee = new Guid(ddlSubCommMembrCommId.SelectedValue);
        }

        protected void ddlCommMembrMembrId_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedMember = new Guid(ddlSubCommMembrMembrId.SelectedValue);
        }
    }
}
